package com.bms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bms.bean.ExamProjectType;
import com.bms.comm.Pagination;
import com.bms.service.ExamProjectTypeService;

@RequestMapping("/exam/projecttype")
@Controller
public class ExamProjectTypeController extends BasicController {
	
	@Resource
	private ExamProjectTypeService examProjectTypeService;
	
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		String pageSize 	   = request.getParameter("pageSize");
		String curPageNo 	   = request.getParameter("curPageNo");
		String projectTypeName = request.getParameter("projectTypeName");
		
		if(pageSize == null || "".equals(pageSize)){
			pageSize = "5";
		}
		if(curPageNo == null || "".equals(curPageNo)){
			curPageNo = "1";
		}
		Pagination<ExamProjectType> pageObj = examProjectTypeService.queryExamProjectTypeForPage(Integer.valueOf(pageSize),Integer.valueOf(curPageNo),projectTypeName);
		List<ExamProjectType> projectTypeList = pageObj.getDataList();
		
		model.addAttribute("pageObj", pageObj);
		model.addAttribute("projectTypeList", projectTypeList);
		model.addAttribute("projectTypeName", projectTypeName);
		return "/exam/project-type-list";
	}
	
	@RequestMapping("/save.htm")
	public String save(String examProjectName,String examProjectDesc) throws Exception{
		examProjectTypeService.saveExamProjectType(examProjectName,examProjectDesc);
		return "redirect:/exam/projecttype/index.htm";
	}
	
	@RequestMapping("/delete.json")
	@ResponseBody
	public Boolean delete(String projectTypeId) {
		boolean flag = true;
		try{
			examProjectTypeService.deleteExamProjectTypeById(projectTypeId);
		}catch(Exception e){
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
}
